Page({
  data: {
    currentContentId: 0,   //默认展示第一个未完成的内容
    purchaseOrderList: [],
    purchaseList:[],
    searchInput:"",
  },
  onLoad() {
    var that = this;
    var date = new Date();
    that.setData({
      purchaseOrderList: [],
    })
    setTimeout(() => {
      this.setData({
        activeIndexDefault: 1
      })
    }, 2000)
    this.getPurchaseOrderList('');   
  },

    // 扫码
    onSearchShop() {
      const that = this;
      dd.scan({
          type: 'qr',
          success: (res) => {
              console.log("扫码结果", res.code);
              that.onSearchInput({ detail: { value: res.code } }); // 调用输入框值改变事件，并传入扫码结果
          },
          fail: (err) => {
              // 扫码失败后的回调函数，可以根据实际需求进行处理
          }
      });
  },
    // 点击了哪个状态
    toApprovalHall(e) {
      const contentId = e.currentTarget.dataset.id; // 获取点击的内容的id
      console.log("我点击了第几个=====>",contentId);
      var that = this;
      that.setData({
        currentContentId: contentId, // 更新当前展示的内容id
        page:1,
        purchaseOrderList:[],
        dowm:true,
      });
  
      if(contentId < 1){
        that.getPurchaseOrderList('');
      }else{
        that.getPurchaseOrderListEnd('');
      }
  
    },
  
    
  
    // 获取输入内容进行搜索
    onSearchInput(e){
      console.log("e===>>",e)
      var that = this;
      that.setData({
        searchInput:e,
        page:1,
        dowm:true,
        purchaseOrderList:[]
      })
      
      that.getPurchaseOrderList();
  
    },
  
  // 用于监听输入框值改变事件
  onSearchInput(e) {
    var that = this;
    var searchTerm = e.detail.value.trim(); // 获取输入框的搜索内容并去除首尾空格
    console.log("输入框用来搜索的内容", searchTerm);
  
    // 清除之前的搜索定时器
    clearTimeout(that.data.searchTimer);
  
    // 设置一个新的定时器，在用户停止输入300毫秒后执行搜索操作
    var searchTimer = setTimeout(function() {
        that.setData({
            searchTimer: null // 清空定时器
        });
        that.getPurchaseOrderList(searchTerm); // 执行搜索操作
        that.getPurchaseOrderListEnd(searchTerm)
    }, 300);
  
    // 将定时器存储在数据中
    that.setData({
        searchTimer: searchTimer
    });
  },
  
  // 清除搜索框内容
  onSearchClear(e) {
    var that = this;
    that.setData({
      searchInput: '', // 清空搜索框内容
      page: 1,
      dowm: true,
      purchaseOrderList: []
    });
  },
  
    // 获取未完成数据
    getPurchaseOrderList(searchTerm) {
      console.log("进入了");
      var that = this;
      // 在请求开始前先清空数组
      that.setData({
        purchaseList: []  // 清空数组
      });
      var data = JSON.stringify({
        "q": {
          "encrypted_text": "0S3OfsOxZaGAVZbShb7iKQ==KgPsr0GMPuWuX3GANFERdLSEGdCN7efJ/4k70cmOruM=",
          "key": "66249c7f42e1e400",
          "sql": "select t1.FID as FID, t1.fbillno as 单据编号,t1.FDate as 收料日期,T3.FNAME as 供应商名称,SUM(t2.FActlandQty) AS 实到数量,COUNT(T2.FMATERIALID) AS 项数,T4.FNAME AS 收料员 from T_PUR_RECEIVE t1 inner join T_PUR_RECEIVEENTRY t2 on t2.fid=t1.fid left join T_BD_SUPPLIER_L t3 on t3.FSUPPLIERID=t1.FSUPPLIERID left join V_BD_WAREHOUSEWORKERS_L t4 on t4.FID=t1.FReceiverId where t1.fbillno like '%" + searchTerm + "%' and t1.FDocumentStatus <>'C' group by t1.FID,t1.fbillno,t1.FDate,T3.FNAME,T4.FNAME order by t1.FID",
          "fields": [
            "FID","单据编号","收料日期","供应商名称","实到数量","项数","收料员"
          ]
        },
        "p": {
          "page_number": 1,
          "page_size": 20
        }
        })
  
      dd.httpRequest({
        url: 'http://cloud.reece-sewing.com:9351/king_sql_query',
        method: 'POST',
        headers: {
          "Content-Type": "application/json"
        },
        data: data,
        dataType: 'json',
        success: function (res) {
          console.log('获取配货单未完成数据响应==>', res.data)
          if (res.data.code == 200) {
          console.log('获取配货单未完成数据响应200000==>', res.data)
            var list = res.data.data;
            var purchaseList = that.data.purchaseList;
            for(var i in list){
              purchaseList.push(list[i])
            }
            if(list.length < that.data.page_size){
              that.setData({
                dowm:false
              })
            }
            that.setData({
              purchaseList:purchaseList
            })
            console.log("数据",purchaseList);
          } else {
            console.log("配货未完成的无数据");
            // dd.showToast({
            //   type: 'fail',
            //   content: res.data.data,
            //   duration: 2000,
            // });
          }
        },
        fail: function (res) {
          // dd.alert({ content: 'fail' });
        },
      });
    },
  
  // 已完成点击查看详情
  godocumentAll(e){
    console.log("e==>",e)
    var item = e.currentTarget.dataset.item
    console.log("已完成的item",item);
    dd.navigateTo({
      url: '/pages/clientMesage/clientMesage?item='+JSON.stringify(item),
    })
  },


});
